<?php
class Core_Model_Session {
	
	/**
	 * first user validation
	 * 
	 * @var $userValidationIntoDatabase
	 */
	public $userValidationIntoDatabase;
	
	/**
	 * user and password validation
	 * 
	 * @var $validationUserPassword
	 */
	public $validationUserPassword;
	
	/**
	 * session data handler
	 * 
	 * @var sessionData
	 */
	public $sessionData;
	
	/**
	 * constructor
	 */
	public function __construct(){
		
		$this->userValidationIntoDatabase = new Core_Data_Userexists();
		$this->validationUserPassword = new Core_Data_Validationuserpassword();
		$this->sessionData = new Core_Data_Sessiondata();
	}
	
	/**
	 * user session initialization
	 * 
	 * @param string $username
	 * @param string $password
	 */
	public function sessionInitialization($username, $password){
		
		if($this->dataValidation($username, $password) === TRUE){
			
			$this->userValidation($username, $password);
			
		}
		else {

			echo '<meta http-equiv="Refresh" content="0;url='.SITE_ROOT.'login.html">';
		
		}
		
	}
	
	/**
	 * user data validation
	 * 
	 * @param string $username
	 * @param string $password
	 */
	public function userValidation($username, $password){
		
		$userData = $this->userValidationIntoDatabase->userValidation($username);
		if((trim($userData['user_username']) == '$username') &&(trim($userData['user_banned']) == "no")){

			$this->getUserData($username, $password);
		
		}
		else {

			echo '<meta http-equiv="Refresh" content="0;url='.SITE_ROOT.'login.html">';
		
		}
		
	}
	
	public function getUserData($username, $password){
		
		if($this->validationUsernameAndPassword($username, $password) === TRUE){
			
			$_SESSION = $this->sessionData->getUSessionData($username, $password);
			
		}
		else {

			echo '<meta http-equiv="Refresh" content="0;url='.SITE_ROOT.'login.html">';
		
		}
		
	}
	
	/**
	 * validation username and password
	 * 
	 * @param string $username
	 * @param string $password
	 * @return boolean
	 */
	public function validiationUsernameAndPassword($username, $password){
		
		$userData = $this->validationUserPassword->validationData($username, $password);
		
		if(((isset($userData['username'])) && (trim($userData['username']) != "")) && ((isset($userData['password'])) && (trim($userData['password']) != ""))){

			return TRUE;
		
		}
		else {
			
			return FALSE;
		
		}
	}
	
	/**
	 * $username && $password cannot be empty
	 * 
	 * @param string $username
	 * @param string $password
	 * @return boolean
	 */
	public function dataValidation($username, $password){
		
		if((trim($username) != "") && (trim($password) != "")){

			return TRUE;
		
		}
		
		else {
			
			return FALSE;
			
		}
	}
	
	
}